<?php
namespace app\houtai\controller\system\channel\room;
use app\houtai\controller\BaseController;
use think\db\Expression;
use Exception;

class KindController extends BaseController {
  /**
   *  @brief  : 渠道游戏服务
   *  @return : page
   *  @details: 
   */
    public function indexAction() {
        $GAME_KIND  = GetGameKind();
        $CHANNELS   = $this->GetAdminChannel();
        $a=array_values($CHANNELS);
        $b= $a[0]['ID'];
        $kind     = $this->request->param('kind/d', 0);
        $channel  = $this->request->param('channel/d',$b);
//        $param = [
//            'name'=>'GetOnlineServer',
//            'channel'=>$channel,
//            'KindID'=>$kind,
//            'ServerID'=>0
//        ];
//        $addr = substr($channel,1,4)+1000;
//        $html = CallGoSrv($param,$addr);//执行句柄，获取返回内容
//        echo "<pre/>";

        $html = '[{"ServerID":1,"Channel":43661,"KindID":50,"RoomNameCN":"13斗地主","Addr":":1990","StartTime":"2019-10-11 17:52:13.9890497","LinksNumber":19,"GameType":16,"NowTimer":1570793438,"Weights":11},{"ServerID":1,"Channel":43661,"KindID":50,"RoomNameCN":"13斗地主","Addr":":1990","StartTime":"2019-10-11 17:52:13.9890497","LinksNumber":19,"GameType":16,"NowTimer":1570793438,"Weights":11},{"ServerID":1,"Channel":43661,"KindID":50,"RoomNameCN":"13斗地主","Addr":":1990","StartTime":"2019-10-11 17:52:13.9890497","LinksNumber":19,"GameType":16,"NowTimer":1570793438,"Weights":11}]';
        $out_arr = is_json($html);
        if (!empty($out_arr)) {
            $html = json_decode($html,true);
            ////////////////////////////////////////////////////////////////////////////
            return $this->fetch('', [
                'GAME_KIND' => $GAME_KIND,
                'CHANNELS'  => $CHANNELS,
                'channel'   => $channel,
                'kind'      => $kind,
                'list'      => $html,
            ]);
        } else {
            return $this->fetch('', [
                'GAME_KIND' => $GAME_KIND,
                'CHANNELS'  => $CHANNELS,
                'channel'   => $channel,
                'kind'      => $kind,
                'list'      => '',
                'error'     => $html,
            ]);
        }
        ///////////////
    }

  /**
   *  @brief  : 房间强制解散
   *  @return : page|JSON
   *  @detail :
   */

  public function dismissAction() {
    if ($this->request->isGet()) {

      return $this->fetch();

    }
    if ($this->request->isPost()) {
      $Agree  = $this->request->param('Agree/d', 0);
      $RoomID = $this->request->param('RoomID/d', 0);
      //--- 验证器验证 ---
      $retn = $this->validate(['RoomID' => $RoomID], [// 按照顺序逐一判断
        'RoomID|六位房间号' => 'require|integer|length:6',
      ]);
      if (true !== $retn) {
        return result([], 100, $retn);
      }
      $Prefix = substr($RoomID, 0, 2);
      $retn = table('GameChannelRoom a', 'rydb.platform')->field([
        'a.Channel', 'b.Name' => 'ChannelName',
        'c.KindID', 'd.KindName',
        'a.ServerID', 'c.ServerName', 'c.ServerPort',
        "(CONVERT (NVARCHAR, c.ServerID + 10) + 'XXXX')" => 'Prefix',
      ])
      ->join('GameChannel b',   'a.Channel = b.ID AND b.State = 1', 'LEFT')
      ->join('GameRoomInfo c',  'a.ServerID = c.ServerID AND c.Nullity = 0', 'LEFT')
      ->join('GameKindItem d',  'c.KindID = d.KindID AND d.Nullity = 0', 'LEFT')
      ->where(['a.State' => 1, ['EXP', new Expression("c.ServerID + 10 = {$Prefix}")]])
      ->find();
      if (empty($retn)) {
        return result([], 101, "[{$RoomID}]没有对应的开服信息");
      }
      
      if (!$Agree) {
        return result($retn, 0);
      } else {
        try { // 通知游戏服务器
          $retn['RoomID'] = $RoomID;
          $retn = CallGameSrv('OnForceKillChannelRoom',$retn);
        } catch (Exception $e) {
          $error = $e->getMessage();
          $errno = $e->getCode();
          return result([], $errno, "通知游戏服务器失败[{$error}]");
        }
        return result([], 0);
      }
    }
  }
}
